package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyCatalogService catalogService;
    @Autowired
    private IStrategyThemeService themeService;
    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Strategy strategy) {
        repository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        repository.save(strategy);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        Query query = new Query();

        //query.addCriteria(Criteria.where("state").is(Strategy.STATE_PUBLISH));

        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }

        //当前页显示数据
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize()
                , Sort.Direction.ASC,"_id");

        return DBHelper.query(query,template,Strategy.class,pageable,qo);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        //先查询
        StrategyCatalog catalog = catalogService.get(strategy.getCatalogId()); //分类的对象
        StrategyTheme theme = themeService.get(strategy.getThemeId()); //查询主题对象

        //替换
        strategy.setDestId(catalog.getDestId()); //目的地id
        strategy.setDestName(catalog.getDestName()); //目的地name
        strategy.setCatalogName(catalog.getName()); //分类名称
        strategy.setThemeName(theme.getName());

        //是否在国外,查询到目的地对象
        //调用吐司方法 查询destId对应的吐司集合
        List<Destination> toasts = destinationService.queryToasts(catalog.getDestId());
        if (toasts != null && toasts.size()>0){
            Destination destination = toasts.get(0);
            //设置是否为国外
            strategy.setIsabroad(destination.getName().equals("中国") ? Strategy.ABROAD_NO : Strategy.ABROAD_YES);
        }
        //再更新
        if (StringUtils.hasLength(strategy.getId())){

            Strategy temp = this.get(strategy.getId());
            strategy.setViewnum(temp.getViewnum()); //阅读量
            strategy.setReplynum(temp.getReplynum());   //评论
            strategy.setFavornum(temp.getFavornum());   //收藏
            strategy.setSharenum(temp.getSharenum());   //分享
            strategy.setThumbsupnum(temp.getThumbsupnum()); //点赞数
            strategy.setCreateTime(temp.getCreateTime()); //创建时间
            this.update(strategy);
        }else {
            strategy.setCreateTime(new Date());
            this.save(strategy);
        }
    }

    @Override
    public List<Strategy> queryByCatalogId(String catalogId) {

        return repository.findByCatalogId(catalogId);
    }

    @Override
    public List<Strategy> queryStrategiesViewnumTop3(String destId) {

        //点击量前3
        Pageable pageable = PageRequest.of(0,3, Sort.Direction.DESC,"viewnum");

        List<Strategy> strategies = repository.findByDestId(destId,pageable);
        return strategies;
    }

    @Override
    public void changeState(int state, String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public void updateStrategyVO(StrategyStatisVO vo) {

        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(vo.getStratrgyId()));
        Update update = new Update();

        update.set("viewnum",vo.getViewnum());
        update.set("replynum",vo.getReplynum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        update.set("favornum",vo.getFavornum());
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> queryHotStrategy() {
        Query query = new Query();
        Pageable pageable = PageRequest.of(0,5, Sort.Direction.DESC,"viewnum");
        query.with(pageable);
        List<Strategy> list = template.find(query,Strategy.class);
        return list;
    }

}
